package andris.android.mobilarena.provider;

import java.util.ArrayList;
import java.util.List;

import andris.android.mobilarena.R;
import andris.android.mobilarena.model.News;
import android.app.Activity;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsListAdapter extends BaseAdapter {

	private List<News> mNews;
	private List<News> mCurrentSelection;
	private LayoutInflater mLayoutInflater;

	public NewsListAdapter(Activity activity, List<News> news) {
		super();
		mNews = news;
		mCurrentSelection = new ArrayList<News>();
		mCurrentSelection.addAll(news);
		mLayoutInflater = activity.getLayoutInflater();

	}

	public void changeSelection(int index) {
		System.out.println("nwes" + mNews.size());
		mCurrentSelection.clear();

		switch (index) {
		case 0:
			for (News n : mNews) {
				if (n.getType())
					mCurrentSelection.add(n);
			}
			System.out.println(mCurrentSelection.size());
			break;
		case 1:
			for (News n : mNews) {
				if (!n.getType())
					mCurrentSelection.add(n);
			}
			System.out.println(mCurrentSelection.size());
			break;
		default:
			mCurrentSelection.addAll(mNews);
			System.out.println(mCurrentSelection.size());
			break;
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mCurrentSelection.size();
	}

	@Override
	public Object getItem(int position) {
		return mCurrentSelection.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView listItem = (TextView) convertView;

		// if (listItem == null)
		listItem = (TextView) mLayoutInflater.inflate(R.layout.news_list_item, null);

		News news = mCurrentSelection.get(position);
		String htmlString = news.getTitle() + "<br/><small>" + news.getDescription() + "</small>";
		listItem.setText(Html.fromHtml(htmlString));
		if (news.getType())
			listItem.setBackgroundColor(Color.DKGRAY);
		else
			listItem.setBackgroundColor(Color.GRAY);
		return listItem;
	}
	
	
}
